package Frame.battleanimation;

import FCRendering.FCRMaster;
import VWorldGameStudio.FCAnimation;
import VWorldGameStudio.FCItem;

import java.awt.*;

import static FCRendering.FCRMaster.fcv;
import static FCRendering.FCRMaster.itemLib;

//战斗使用工具
public class UseToolAnimation extends ABattleAni {
    FCAnimation fcAnimation;
    int[] startpoints;
    int[] firePaintPoints;
    private int pngIndex = -1;
    private FCItem toolItem;
    int[] toolItemXy = new int[2];
    public UseToolAnimation(String itemName, int[] startPoint, boolean isPlayerAttackMode) {
        this.startpoints = startPoint;
        toolItem = itemLib.getItemOfID(itemName);//使用什么工具
        this.isPlayerAttackMode = isPlayerAttackMode;
        fcAnimation = FCRMaster.animationLib.getAnimationOfID("battelUseToolAnimation");

        if (isPlayerAttackMode) {
            playerOffset[0] = (int) (20 * fcv.getProportion());
            startPoint[0] -= playerOffset[0];
        } else {
            playerOffset[0] = (int) (40 * fcv.getProportion());
            startPoint[0] += playerOffset[0];
        }
        if (fcAnimation != null && fcAnimation.getFirePngUrl().size() > 0) {
            allFrames = fcAnimation.getFireFrameTime() / 16;
        } else {
            allFrames = 20;
        }
    }

    @Override
    public void nextFrame(int frame) {
        if(fcAnimation != null) {
            int p = allFrames / fcAnimation.getFirePngUrl().size();
            int index = frame / p + 1;
            pngIndex = Math.min(index, fcAnimation.getFirePngUrl().size() - 1);
            if (firePaintPoints == null) {
                firePaintPoints = new int[2];
                firePaintPoints[0] = startpoints[0] - (int) (fcAnimation.firePngSizeW * fcv.getProportion())/2;
                firePaintPoints[1] = startpoints[1] - (int) (fcAnimation.firePngSizeH * fcv.getProportion());

                toolItemXy[0] = startpoints[0] - (int) (40 * fcv.getProportion())/2;
                toolItemXy[1] = firePaintPoints[1]+(int) (fcAnimation.firePngSizeH * fcv.getProportion())/2-(int) (40 * fcv.getProportion())/2;
            }
        }
    }

    @Override
    public void paintBattleSceen(Graphics g, FCRMaster fcrMaster) {
        if(pngIndex != -1) {
            g.drawImage(fcAnimation.getFirePng().get(pngIndex), firePaintPoints[0], firePaintPoints[1],
                    (int) (fcAnimation.firePngSizeW * fcv.getProportion()), (int) (fcAnimation.firePngSizeH * fcv.getProportion()), fcrMaster);
            g.drawImage(toolItem.getPngPng(), toolItemXy[0], toolItemXy[1],
                    (int) (40 * fcv.getProportion()), (int) (40 * fcv.getProportion()), fcrMaster);

        }
    }
}
